package com.atguigu.rabbit.producer.config;

import jakarta.annotation.PostConstruct;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitTemplateConfiguration implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String mes) {
        System.out.println("==================confirm===================");
        // 确认状态 produce --中间发生--> exchange
        System.out.println(ack);
        System.out.println("==================confirm===================");
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        // 回调状态 exchange --中间发生--> queue
        System.out.println("==================return===================");
        byte[] body = returnedMessage.getMessage().getBody();
        String msg = new String(body);
        String messageId = returnedMessage.getMessage().getMessageProperties().getHeader("spring_returned_message_correlation");
        System.out.println("messageId:"+messageId);
        System.out.println(msg);
        System.out.println("==================return===================");

    }
}
